package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5733
 * http://www.2500sz.com/  名城新闻网
 *
 * @author KK
 *
 */
public class _2500szNewsAnalyse implements AnalyseNews {
//http://news.2500sz.com/news/szxw/xsxw/2014/1/9/2253609.shtml
	//http://news.2500sz.com/news/gn/ty/2259302.shtml
	//http://eng.2500sz.com/english/news/national_news/2014-1-16/2259390.shtml
	//http://news.2500sz.com/news/szxw/xsxw/tcs/xwsd/2013/12/30/2245876.shtml
	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/

		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[0-9]*-[0-9]*/[0-9]*_[0-9]*.shtml",
		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[0-9]*/[0-9]*/[0-9]*/[0-9]*.shtml",
		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[a-z]*\\_?[a-z]*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/[0-9]*.shtml",
		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[a-z]*/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9]*.shtml",
		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[a-z]*/[0-9]*.shtml",
		"http://[a-z]*.2500sz.com/[a-z]*/[a-z]*/[a-z]*/[a-z]*/[a-z]*/[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9]*.shtml",
		"http://.*.2500sz.com/.*/[0-9]{5,}.shtml",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}

		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		doc.select(".ny-nav").remove();
		doc.select(".doclist").remove();


		title =doc.select("div.right-center p.title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.dbt h1").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("title").text();
			if (title!=null)title=StringUtils.substringBefore(title, "-");
		}


		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源:");

		date=JSoupUtils.matchDate(doc, "时间:","来源:");

		if(date == null){
			date=DateUtils.matchDate(doc.select("h2 span").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.content-article h3").text());
		}
		if(date == null){
			date=DateUtils.matchDate(url);
		}

		doc.select("div#content-text h1").remove();
		doc.select("div#content-text .time").remove();
		content= HtmlCleaner.getContentHtml(url,doc.select("div#content-text,div.pic"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.con p"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("section.con"));
		}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
			news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://news.2500sz.com/news/tmsz/2689778.shtml";
		_2500szNewsAnalyse analyse=new _2500szNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
